package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.LeaveStudent;
import cn.wolfcode.crm.domain.Officialstu;
import cn.wolfcode.crm.query.OfficialstuQueryForObject;
import cn.wolfcode.crm.query.ReportQueryForObject;
import cn.wolfcode.crm.service.IOfficialstuService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import com.alibaba.fastjson.JSON;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 正式学员
 */
@Controller
@RequestMapping("officialstu")
public class OfficialstuController {
    @Autowired
    private IOfficialstuService officialstuService;

    @PermissionName("正式学员首页")
    @RequiresPermissions("officialstu:index")
    @RequestMapping("index")
    public String index(){
        return "officialstu/list";
    }

    @PermissionName("正式学员列表")
    @RequiresPermissions("officialstu:list")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(OfficialstuQueryForObject qo){
        return officialstuService.query(qo);
    }

    @PermissionName("正式学员编辑")
    @RequiresPermissions("officialstu:edit")
    @RequestMapping("edit")
    @ResponseBody
    public JsonResult edit(Officialstu record){
        try{
            officialstuService.updateByPrimaryKey(record);
        }catch (Exception e){
            e.printStackTrace();
            new JsonResult("操作失败");
        }
        return new JsonResult();
    }

    //查所有未缴清学费的学员
    @RequestMapping("selectNoPayAll")
    @ResponseBody
    public List<Officialstu> selectNoPayAll(){
        return officialstuService.selectNoPayAll();
    }

    //根据id查学员的部分信息
    @RequestMapping("queryById")
    @ResponseBody
    public Officialstu queryById(Long id){
        return officialstuService.queryById(id);
    }

    /**
     * 修改状态
     * @param state
     * @return
     */
    @PermissionName("正式学员状态改变")//1在读 2转班中 3休学 4退学
    @RequiresPermissions("officialstu:changeState")
    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changeState(Long stuId, Integer state){
        try{
            officialstuService.changeState(stuId, state);
        }catch (Exception e){
            e.printStackTrace();
            new JsonResult("操作失败");
        }
        return new JsonResult();
    }

    /**
     * 根据学员id改变学员班级和状态
     * @param stuId
     * @param newGradeId
     * @param state
     * @return
     */
    @RequestMapping("changeClasses")
    @ResponseBody
    public JsonResult changeClasses(Long stuId, Long newGradeId, Integer state){
        try{
            officialstuService.changeClasses(stuId, newGradeId, state);
        }catch (Exception e){
            e.printStackTrace();
            new JsonResult("操作失败");
        }
        return new JsonResult();
    }



    @RequestMapping("allList")
    @ResponseBody
    public List<Officialstu> allList(){
        return officialstuService.selectAll();
    }

    /**
     *
     * @return
     */
    @RequestMapping("selectAllStudentByBigClient")
    @ResponseBody
    public int selectAllStudentByBigClient(Long id){
        return officialstuService.selectAllStudentByBigClient(id);
    }

    //根据学员流失表退款状态查到对应学员
    @ResponseBody
    @RequestMapping("selectAllByRefundState")
    public List<Officialstu> selectAllByRefundState(){
        return officialstuService.selectAllByRefundState();
    }

    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception{
        //导出
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String fileName = sdf.format(new Date()) + ".xls";
        response.setContentType("application/x-excel");
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="
                + fileName);
        List<Officialstu> list = new ArrayList<>();
        List<Officialstu> officialstus = officialstuService.selectAll();
        for (Officialstu officialstu : officialstus) {
            list.add(officialstu);
        }
        //1.创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
        //2.创建页签
        WritableSheet sheet = workbook.createSheet("员工列表", 0);
        //3.创建单元格
        String[] header = new String[]{"学员名称","销售人员","入学时间","总学费","已交学费","未缴学费","缴费状态","学校","电话","邮箱","班级","支付方式","状态"};
        Label label = null;
        for (int i = 0; i < header.length; i++) {
            label = new Label(i,0,header[i]);
            //4.添加到页签中
            sheet.addCell(label);
        }
        //迭代数据
        for (int i = 1; i <list.size()+1 ; i++) {
            label = new Label(0,i,list.get(i-1).getName());
            sheet.addCell(label);
            if (list.get(i-1).getMarketer()==null){
                label = new Label(1,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(1, i, list.get(i - 1).getMarketer().getUsername());
                sheet.addCell(label);
            }
            if (list.get(i-1).getPositiveTime()==null){
                label = new Label(2, i, "");
                sheet.addCell(label);
            }else {
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = sdf.format(list.get(i - 1).getPositiveTime());
                label = new Label(2, i, time);
                sheet.addCell(label);
            }
            if (list.get(i-1).getTotalFee()==null){
                label = new Label(3,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(3, i, list.get(i - 1).getTotalFee().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                sheet.addCell(label);
            }
            if (list.get(i-1).getPaidFee()==null){
                label = new Label(4,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(4, i, list.get(i - 1).getPaidFee().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                sheet.addCell(label);
            }
            if (list.get(i-1).getNoPay()==null){
                label = new Label(5,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(5, i, list.get(i - 1).getNoPay().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                sheet.addCell(label);
            }
            label = new Label(6,i,list.get(i-1).getFeeState().toString());
            sheet.addCell(label);
            label = new Label(7,i,list.get(i-1).getSchool());
            sheet.addCell(label);
            label = new Label(8,i,list.get(i-1).getTel());
            sheet.addCell(label);
            label = new Label(9,i,list.get(i-1).getEmail());
            sheet.addCell(label);
            if (list.get(i-1).getIttclass()==null){
                label = new Label(10,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(10, i, list.get(i - 1).getIttclass().getName());
                sheet.addCell(label);
            }
            if (list.get(i-1).getPayment()==null){
                label = new Label(11,i,"");
                sheet.addCell(label);
            }else {
                label = new Label(11, i, list.get(i - 1).getPayment().getName());
                sheet.addCell(label);
            }
            label = new Label(12, i, list.get(i - 1).getState().toString());
            sheet.addCell(label);
        }
        //5.写出数据
        workbook.write();
        workbook.close();
    }

    @PermissionName("报表首页")
    @RequiresPermissions("officialstu:inreport")
    @RequestMapping("inreport")
    public String inreport(ReportQueryForObject qo){
        return "officialstu/report";
    }
    @PermissionName("报表列表")
    @RequiresPermissions("officialstu:report")
    @RequestMapping("report")
    @ResponseBody
    public List<Map<String, Object>> report(ReportQueryForObject qo){
        List<Map<String, Object>> maps = officialstuService.selectGroupBy(qo);
        return maps;
    }
}
